/**
 * Free software.
 */

package org.nanhill.moo.service.impl;

import java.util.Collection;

import org.nanhill.moo.dao.CategoryDao;
import org.nanhill.moo.domain.Category;
import org.nanhill.moo.service.InternalCategoryService;

/**
 * @author alin [xalinx at gmail dot com]
 * @date 2006-10-21
 */
public class CategoryServiceImpl extends AbstractService implements
		InternalCategoryService {

	private CategoryDao categoryDao;

	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	private CategoryCache rootChildsCache;


	public CategoryServiceImpl() {
		final InternalCategoryService thisService = this;
		rootChildsCache = new CategoryCache() {
			{
				setCacheMeantime(CONFIG.getCategoryChildsCacheMeantime());
			}

			@Override
			protected void build() {
				cache = thisService.loadRootChilds();
			}
		};
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.moo.service.CategoryService#getChilds(long)
	 */
	public Collection<Category> loadCachedRootChilds() {
		rootChildsCache.trybuild();
		return rootChildsCache.getCache();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.moo.service.CategoryService#load(long)
	 */
	public Category loadById(long cid) {
		return categoryDao.findById(cid);
	}

	public Collection<Category> loadRootChilds() {
		return categoryDao.findRootChilds();
	}

}
